package de.ttf.ts.control.exception;


/**
 * Exception für "technischen" Fehlern
 * 
 * @author ub8uhm
 * 
 */
public class ServiceException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private String host;
    private String aktion;

    public ServiceException(String message, Throwable t) {
        super(message, new ExceptionProxy(t));
    }

    public ServiceException(Throwable t) {
        super(new ExceptionProxy(t));
    }

    public ServiceException(String message) {
        super(message);
    }

    public String getHost() {
        return host;
    }

    public String getAktion() {
        return aktion;
    }

    public void setHostAndAktion(String host, String aktion) {
        this.host = host;
        this.aktion = aktion;
    }

    @Override
    public String toString() {
        String host = this.host != null ? " | Host: " + this.host : "";
        String aktion = this.aktion != null ? " | Aktion: " + this.aktion : "";
        return super.toString() + host + aktion;
    }

    private static class ExceptionProxy extends Throwable {

        private static final long serialVersionUID = 1L;
        private String throwableClassName;

        private ExceptionProxy(Throwable t) {
            super(t.getLocalizedMessage());
            this.throwableClassName = t.getClass().getName();
            setStackTrace(t.getStackTrace());
            if (t.getCause() != null) {
                initCause(new ExceptionProxy(t.getCause()));
            }
        }

        @Override
        public String toString() {
            String s = this.throwableClassName;
            String message = getLocalizedMessage();
            return (message != null) ? (s + ": " + message) : s;
        }

    }
}
